/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.cud.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.iwsh.core.utils.IDGenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.us.usws.biz.ResourceStorageReplacer;
import com.us.usws.dao.VideoDAO;
import com.us.usws.dao.VideoFileDAO;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.service.cud.VideoFileCUDService;

/**
 * Service RO impl class
 * @author robot
 */
public class VideoFileCUDServiceImpl implements VideoFileCUDService {

	/**
	 * DAO instance
	 */
	private VideoFileDAO dao = new VideoFileDAO();
	
	private VideoDAO videoDao = new VideoDAO();

	/**
	 * Insert one row.
	 */
	@Override
	public String addVideoFile(VideoFile videoFile) {
		/*
		 * create video file uid
		 */
		String videoFileUid = IDGenerator.getInstance().genUID();
		videoFile.setUid(videoFileUid);
		
		String snapshotPath = videoFile.getSnapshot();
		String videoPath = videoFile.getVideoPath();
		try {
			if(StringUtils.isNotBlank(snapshotPath)) {
				int index = snapshotPath.lastIndexOf("/");
			  	if(index != -1) {
			  		videoFile.setSnapshot(snapshotPath.substring(index+1));
			  		FileUtils.copyFile(new File(snapshotPath), new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
			  	} 
             }
			if(StringUtils.isNotBlank(videoPath)) {
				FileUtils.copyFile(new File(videoPath), new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}
			
			/*
			 * when file copy is success, then insert video info
			 */
			dao.insertVideoFile(videoFile);
			return videoFileUid;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Update by PK.
	 */
	@Override
	public void updateVideoFile(VideoFile videoFile) {
		String snapshotPath = videoFile.getSnapshot();
		String videoPath = videoFile.getVideoPath();
		try {
			if(StringUtils.isNotBlank(snapshotPath) && snapshotPath.contains("/")) {
				int index = snapshotPath.lastIndexOf("/");
			  	if(index != -1) {
			  		videoFile.setSnapshot(snapshotPath.substring(index+1));
			  		FileUtils.copyFile(new File(snapshotPath), new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
			  	} 
			}
			if(StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
				FileUtils.copyFile(new File(videoPath), new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		dao.updateVideoFile(videoFile);
	}

	/**
	 * Delete row by PK.
	 */
	@Override
	public boolean deleteVideoFile(String uid) {
		int count = videoDao.getVideoCountByFileUid(uid);
		if(count == 0) {
			dao.deleteVideoFile(uid);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<String> deleteVideoFileMore(String uids) {
		List<String> videoFileUids = null;
		if (StringUtils.isNotBlank(uids)) {
			videoFileUids = new ArrayList<String>();
			String[] uid = StringUtils.split(uids, ",");
			for (String id : uid) {
				if(!deleteVideoFile(id)) {
					videoFileUids.add(id);
				}
			}
		}
		return videoFileUids;
	}

}